package com.volunteer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.common.R;
import com.volunteer.entity.Activity;
import com.volunteer.model.vo.ActivityDetailVO;
import com.volunteer.model.vo.ActivityListVO;
import com.volunteer.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 活动控制器
 */
@RestController
@RequestMapping("/api/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 获取活动列表
     */
    @GetMapping("/list")
    public CompletableFuture<R<List<ActivityListVO>>> getActivityList(@RequestParam(required = false) Integer status, @RequestParam(required = false) Boolean isRecommended, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return activityService.getActivityList(status, isRecommended, page, size).thenApply(R::ok);
    }

    /**
     * 获取活动详情
     */
    @GetMapping("/{id}")
    public CompletableFuture<R<ActivityDetailVO>> getActivityDetail(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        return activityService.getActivityDetail(id, userId).thenApply(R::ok);
    }

    /**
     * 创建活动
     */
    @PostMapping
    public R<Boolean> createActivity(@RequestBody Activity activity) {
        activityService.createActivity(activity);
        return R.ok(true);
    }

    /**
     * 更新活动
     */
    @PutMapping("/{id}")
    public R<Boolean> updateActivity(@PathVariable Long id, @RequestBody Activity activity) {
        activity.setId(id);
        activityService.updateActivity(activity);
        return R.ok(true);
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public CompletableFuture<R<Boolean>> deleteActivity(@PathVariable Long id) {
        return activityService.deleteActivity(id).thenApply(R::ok);
    }

    /**
     * 推荐/取消推荐活动
     */
    @PutMapping("/{id}/recommend")
    public CompletableFuture<R<Boolean>> recommendActivity(@PathVariable Long id, @RequestParam Boolean isRecommended) {
        return activityService.recommendActivity(id, isRecommended).thenApply(R::ok);
    }

    /**
     * 修改活动状态
     */
    @PutMapping("/{id}/status")
    public CompletableFuture<R<Boolean>> updateActivityStatus(@PathVariable Long id, @RequestParam Integer status) {
        return activityService.updateActivityStatus(id, status).thenApply(R::ok);
    }

    /**
     * 获取我创建的活动
     */
    @GetMapping("/created")
    public CompletableFuture<R<List<ActivityListVO>>> getCreatedActivities(@RequestParam Long userId, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return activityService.getCreatedActivities(userId, page, size).thenApply(R::ok);
    }

    /**
     * 获取热门活动
     */
    @GetMapping("/hot")
    public CompletableFuture<R<List<ActivityListVO>>> getHotActivities(@RequestParam(defaultValue = "5") Integer limit) {
        return activityService.getHotActivities(limit).thenApply(R::ok);
    }

    /**
     * 获取用户参与的活动
     */
    @GetMapping("/joined")
    public CompletableFuture<R<List<ActivityListVO>>> getJoinedActivities(@RequestParam Long userId, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return activityService.getJoinedActivities(userId, page, size).thenApply(R::ok);
    }

    /**
     * 获取活动总数
     */
    @GetMapping("/count")
    public CompletableFuture<R<Integer>> getActivityCount() {
        return activityService.getActivityCount().thenApply(R::ok);
    }

    /**
     * 分页查询活动
     */
    @GetMapping("/page")
    public R<Page<Activity>> pageActivities(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size, Activity activity) {
        Page<Activity> page = new Page<>(current, size);
        Page<Activity> result = activityService.pagePublicActivities(page, activity);
        return R.ok(result);
    }
}
